﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

using ArticleManager.DataClasses;
using ArticleManager.Helpers;
using ArticleManager.Forms;

namespace ArticleManager
{
    public sealed class Controller
    {
        static Controller _instance = null;
        static readonly object padlock = new object();
        Logger errorLog;
        DataHelper dh;
        IList articleTitles;
        Article _currentArticle;// = new Article();
        private  frmMain mainForm;

        private Controller()
        {
            dh = new DataHelper();
            errorLog = new Logger();
            articleTitles = new ArticleTitlesCollection();
            articleTitles.Clear();
            _currentArticle = new Article();
        }


        public frmMain ArticleForm
        {
            set
            {
                mainForm = value;
            }
        }


        public static Controller Instance
        {
            get
            {
                lock (padlock)
                {
                    if (_instance == null)
                    {
                        _instance = new Controller();
                    }
                    return _instance;

                }
            }
        }

        #region Data Handling functions.
        public IList GetArticleTitles()
        {
            return this.dh.GetArticleTitles();
        }

        public Article GetArticleDetails(int id)
        {
            _currentArticle = dh.GetArticle(id);
            return _currentArticle;
        }

        public string GetKeyText(int id)
        {
            return dh.GetKeyText(id);
        }

        public string GetAuthorName(int id)
        {
            return dh.GetAuthorName(id);
        }

        public IList GetKeywords()
        {
            return dh.GetKeywords();
        }

        public IList GetAuthors()
        {
            return dh.GetAuthors();
        }

        public void AddNewArticle(ref Article article)
        {
            if (dh.AddNewArticle(ref article))
            {
                mainForm.AddArticleToList(article.ArticleId, article.Title);
            }
        }
        public void UpdateArtcile(ref Article article)
        {
            dh.UpdateArtcile(ref article);
            //if (dh.UpdateArtcile(ref article))
            //{
            //    mainForm.AddArticleToList(article.ArticleId, article.Title);
            //}
        }
        #endregion.


        //pri DataHelper DataHelper
        //{
        //    get
        //    {
        //        return this.dh;
        //    }
        //}



        #region Error Logging.
        public void LogError(string strMessage)
        {
            errorLog.WriteToLog(strMessage);
        }

        public void LogException(Exception ex)
        {
            LogError(ex.Message);
        }
        #endregion
    }
}
